package com.example.filesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.filesystem.entity.FileInfo;
import com.example.filesystem.entity.Result;
import com.example.filesystem.service.IFileInfoService;
import com.example.filesystem.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;

import static com.example.filesystem.util.FileUtil.md5ToBytes;

/**
 * @author bbyh
 * @since 2025-04-27
 */
@RestController
@RequestMapping("/file-info")
@Slf4j
@Api("/文件接口")
public class FileInfoController {
    @Resource
    private IFileInfoService fileInfoService;
    @Resource
    private IUserService userService;

    @ApiOperation("生成文件接口")
    @PostMapping("/generateFile")
    public Result generateFile(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        try {
            String username = request.getHeader("username");
            Integer dirId = (Integer) map.get("dirId");
            String fileName = (String) map.get("fileName");
            String fileMd5 = (String) map.get("fileMd5");
            Integer fileSize = (Integer) map.get("fileSize");

            FileInfo fileInfo = new FileInfo();
            fileInfo.setUserId(userService.getUserIdByUsername(username));
            fileInfo.setDirId(dirId);
            fileInfo.setFileMd5(md5ToBytes(fileMd5));
            fileInfo.setFileName(fileName);
            fileInfo.setFileSize(fileSize);
            fileInfo.setCreateTime(LocalDateTime.now());
            fileInfoService.save(fileInfo);

            return Result.success(null, "文件：" + fileName + "上传成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(null, "生成文件接口服务出错");
        }
    }

    @ApiOperation("文件md5校验接口")
    @GetMapping("/checkMd5")
    public Result checkMd5(@RequestParam String fileMd5) {
        try {
            LambdaQueryWrapper<FileInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(FileInfo::getFileMd5, md5ToBytes(fileMd5));
            FileInfo fileInfo = fileInfoService.getOne(wrapper);
            if (fileInfo != null) {
                return Result.success("文件MD5已存在", null);
            } else {
                return Result.error("文件MD5不存在", null);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(null, "文件md5校验接口服务出错");
        }
    }

    @ApiOperation("判断目录下是否存在该名称的文件接口")
    @PostMapping("/judgeFileExist")
    public Result judgeFileExist(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        try {
            String username = request.getHeader("username");
            Integer dirId = (Integer) map.get("dirId");
            String fileName = (String) map.get("fileName");

            LambdaQueryWrapper<FileInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(FileInfo::getUserId, userService.getUserIdByUsername(username)).eq(FileInfo::getDirId, dirId).eq(FileInfo::getFileName, fileName);
            FileInfo fileInfo = fileInfoService.getOne(wrapper);
            if (fileInfo == null) {
                return Result.success(null, "该文件名称不存在");
            } else {
                return Result.error(null, "该文件名称已在当前文件夹内存在");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(null, "判断目录下是否存在该名称的文件接口服务出错");
        }
    }
}
